﻿Imports SLFileManager.FileManagerService

Namespace MyConverters
    Public Class ImageSourceConverter
        Implements Data.IValueConverter

        Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) Implements Data.IValueConverter.Convert
            Dim src As String = Application.Current.Host.Source.ToString()
            Dim applicationRoot As String = src.Substring(0, src.IndexOf("ClientBin"))
            Dim filename As String = ""
            If value.GetType Is GetType(String) Then
                If String.IsNullOrEmpty(value) Then
                    filename = value.ToString()
                Else
                    filename = GetImgSourceFromExt(value.ToString())
                End If
            ElseIf value.GetType Is GetType(ServerFile) Then
                filename = GetImgSourceFromExt(DirectCast(value, ServerFile)._URLPath & _
                      DirectCast(value, ServerFile)._FileName)
            End If
            Dim url As Uri
            If getExtFromFilename(filename) = "gif" Then
                url = New Uri(applicationRoot & "ConvertGif.ashx?link=" & filename, UriKind.Absolute)
            Else
                ''Where CLientBin is the location of your XAP file
                url = New Uri(applicationRoot & filename, UriKind.Absolute)
            End If
            Return New Imaging.BitmapImage(url)
        End Function
        Private Function GetImgSourceFromExt(ByVal filename As String) As String
            Dim ext As String = getExtFromFilename(filename)
            Select Case ext.ToLower
                Case "jpg", "jpeg", "png"
                    Return filename
                Case "gif"
                    Return filename '"Images/Mediatypes/gif.png"
                Case "wmv", "mov", "flv", "mpg", "avi", "mpeg", "divx", "ism", "asx"
                    Return "Images/Mediatypes/video.png"
                Case "wma", "mp3", "fla"
                    Return "Images/Mediatypes/audio.png"
                Case "txt", "css"
                    Return "Images/Mediatypes/text.png"
                Case "pdf"
                    Return "Images/Mediatypes/pdf.png"
                Case "html", "htm"
                    Return "Images/Mediatypes/html.png"
                Case "doc", "docx"
                    Return "Images/Mediatypes/word.png"
                Case "ppt", "pptx"
                    Return "Images/Mediatypes/powerpoint.png"
                Case "xls", "xlsx"
                    Return "Images/Mediatypes/excel.png"
                Case Else
                    Return ""
            End Select
        End Function
        Private Function getExtFromFilename(ByVal filename) As String
            Dim ext As Text.RegularExpressions.Match = Text.RegularExpressions.Regex.Match(filename, "^.+\.([^.]+)$")
            If ext.Success Then
                Return ext.Groups(1).Value
            Else
                Return ""
            End If
        End Function
        Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) Implements Data.IValueConverter.ConvertBack
            Throw New NotImplementedException()
        End Function
    End Class

    Public Class TreeViewNodeStyleConverter
        Implements Data.IValueConverter

        Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) Implements Data.IValueConverter.Convert
            If value Is Nothing OrElse _
                DirectCast(value, Collections.ObjectModel.ObservableCollection(Of ServerFile)).Count <= 0 Then
                Return App.Current.Resources("TreeViewNodeStyleEmpty")
            Else
                Return App.Current.Resources("TreeViewNodeStyleFull")
            End If
        End Function
        Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) Implements Data.IValueConverter.ConvertBack
            Throw New NotImplementedException()
        End Function
    End Class
End Namespace